package com.woniu108.person.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.carService.commos.dto.StoreApplyDto;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.person.mapper.StoreApplyMapper;
import com.woniu108.person.model.StoreApply;
import com.woniu108.person.param.StoreApplyListParam;
import com.woniu108.person.service.StoreAdminService;
import com.woniu108.person.service.StoreApplyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 门店新增申请表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class StoreApplyServiceImpl extends ServiceImpl<StoreApplyMapper, StoreApply> implements StoreApplyService {

    @Resource
    private StoreApplyMapper storeApplyMapper;
    @Resource
    private StoreAdminService storeAdminService;
    @Override
    public Page<StoreApplyDto> storeApplyList(StoreApplyListParam param) {
        //根据筛选条件获取storeApply集合
        QueryWrapper<StoreApply> wrapper = new QueryWrapper<>();
        if(param.getStoreAdminId()!=null) wrapper.like("store_admin_id",param.getStoreAdminId());
        if(param.getStoreName()!=null) wrapper.like("store_name",param.getStoreName());
        wrapper.orderByDesc("create_time");
        wrapper.ne("apply_state", StaticData.STORE_APPLY_STATE_DELETE);
        List<StoreApply> storeApplyList = storeApplyMapper.selectList(wrapper);
        //将storeApply转为dto放入新集合
        List<StoreApplyDto> list = new ArrayList<>();
        for (StoreApply storeApply : storeApplyList) {
            StoreApplyDto dto = BeanUtil.toBean(storeApply, StoreApplyDto.class);
           dto.setStoreAdminName(storeAdminService.getById(storeApply.getStoreAdminId()).getStoreAdminName());
            list.add(dto);
        }
        //分页
        long total = list.size();
        long pages=total%param.getPageSize()==0?total/param.getPageSize():total/param.getPageSize()+1;
        Page<StoreApplyDto> page=new Page<StoreApplyDto>();
        List<StoreApplyDto> dtos=new ArrayList<>();
        int i=0;
        for (StoreApplyDto infoDto : list) {
            if(i<param.getPageNum()*param.getPageSize().intValue()&&i>=(param.getPageNum()-1)*param.getPageSize().intValue()){
                dtos.add(infoDto);
            }
            i=i+1;
        }
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        page.setRecords(dtos);
        page.setTotal(total);
        page.setPages(pages);
        return page;
    }

    @Override
    public Page<StoreApplyDto> storeApplyListStateNormal(StoreApplyListParam param) {
        //根据筛选条件获取storeApply集合
        QueryWrapper<StoreApply> wrapper = new QueryWrapper<>();
        if(param.getStoreAdminId()!=null) wrapper.like("store_admin_id",param.getStoreAdminId());
        if(param.getStoreName()!=null) wrapper.like("store_name",param.getStoreName());
        wrapper.orderByDesc("create_time");
        wrapper.eq("apply_state", StaticData.STORE_APPLY_STATE_WAIT);
        List<StoreApply> storeApplyList = storeApplyMapper.selectList(wrapper);
        //将storeApply转为dto放入新集合
        List<StoreApplyDto> list = new ArrayList<>();
        for (StoreApply storeApply : storeApplyList) {
            StoreApplyDto dto = BeanUtil.toBean(storeApply, StoreApplyDto.class);
            dto.setStoreAdminName(storeAdminService.getById(storeApply.getStoreAdminId()).getStoreAdminName());
            list.add(dto);
        }
        //分页
        long total = list.size();
        long pages=total%param.getPageSize()==0?total/param.getPageSize():total/param.getPageSize()+1;
        Page<StoreApplyDto> page=new Page<StoreApplyDto>();
        List<StoreApplyDto> dtos=new ArrayList<>();
        int i=0;
        for (StoreApplyDto infoDto : list) {
            if(i<param.getPageNum()*param.getPageSize().intValue()&&i>=(param.getPageNum()-1)*param.getPageSize().intValue()){
                dtos.add(infoDto);
            }
            i=i+1;
        }
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        page.setRecords(dtos);
        page.setTotal(total);
        page.setPages(pages);
        return page;
    }


    @Override
    public Page<StoreApplyDto> storeApplyListStateDealed(StoreApplyListParam param) {
        //根据筛选条件获取storeApply集合
        QueryWrapper<StoreApply> wrapper = new QueryWrapper<>();
        if(param.getStoreAdminId()!=null) wrapper.like("store_admin_id",param.getStoreAdminId());
        if(param.getStoreName()!=null) wrapper.like("store_name",param.getStoreName());
        wrapper.orderByDesc("create_time");
        wrapper.eq("apply_state",StaticData.STORE_APPLY_STATE_ALLOW).or().eq("apply_state",StaticData.STORE_APPLY_STATE_NOT_ALLOW);
        List<StoreApply> storeApplyList = storeApplyMapper.selectList(wrapper);
        //将storeApply转为dto放入新集合
        List<StoreApplyDto> list = new ArrayList<>();
        for (StoreApply storeApply : storeApplyList) {
            StoreApplyDto dto = BeanUtil.toBean(storeApply, StoreApplyDto.class);
            dto.setStoreAdminName(storeAdminService.getById(storeApply.getStoreAdminId()).getStoreAdminName());
            list.add(dto);
        }
        //分页
        long total = list.size();
        long pages=total%param.getPageSize()==0?total/param.getPageSize():total/param.getPageSize()+1;
        Page<StoreApplyDto> page=new Page<StoreApplyDto>();
        List<StoreApplyDto> dtos=new ArrayList<>();
        int i=0;
        for (StoreApplyDto infoDto : list) {
            if(i<param.getPageNum()*param.getPageSize().intValue()&&i>=(param.getPageNum()-1)*param.getPageSize().intValue()){
                dtos.add(infoDto);
            }
            i=i+1;
        }
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        page.setRecords(dtos);
        page.setTotal(total);
        page.setPages(pages);
        return page;
    }
}
